package com.unieagle.exam.ui;

import java.util.List;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.unieagle.exam.R;
import com.unieagle.exam.db.DatabaseContext;
import com.unieagle.exam.db.ExamDBHelper;
import com.unieagle.exam.model.Exam;
import com.unieagle.exam.model.Question;


/**
 * 考试结果页面
 * @author QianJing
 *
 */

public class ExamResultActivity extends BaseActivity {
	
	private Exam mExam;
	
	private TextView mTvResult, mTvIsPass, mTvAverage, mTvHighest, mTvExamCnt;
	
	private Button mBtnReview, mBtnErrorExam;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.exam_result_activity_layout);
		init();
	}
	
	protected void init() {
		super.init();
		mTvTitle.setText(R.string.exam_result_hint);
		mTvResult = (TextView)findViewById(R.id.result);
		mTvIsPass = (TextView) findViewById(R.id.pass);
		mTvAverage = (TextView) findViewById(R.id.average);
		mTvHighest = (TextView) findViewById(R.id.highest);
		mTvExamCnt = (TextView) findViewById(R.id.exam_cnt);
		mBtnReview = (Button) findViewById(R.id.review);
		mBtnReview.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				reviewExam();				
			}
		});
		mExam = (Exam)getIntent().getParcelableExtra("exam");
		if(mExam != null) {
			initResult();
		}
		mBtnErrorExam = (Button) findViewById(R.id.error_exam);
		mBtnErrorExam.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				errorExam();
			}
		});
		mBBBack.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}
	
	private void initResult() {
		mTvResult.setText(mExam.getScore()+ getString(R.string.f));
		if(mExam.isPass()) {
			mTvIsPass.setText(R.string.pass);
		} else {
			mTvIsPass.setText(R.string.impass);
		}
		ExamDBHelper helper = new ExamDBHelper(this);
		List<Integer> result = helper.getExamResultByCaseId(mExam.getCaseId());
		mTvExamCnt.setText("" + result.size());
		int highest = 0, total = 0;
		for(Integer score : result) {
			if(score > highest) {
				highest = score;
			}
			total += score;
		}
		mTvAverage.setText("" + (total / result.size()));
		mTvHighest.setText("" + highest);
		new CalErrorTimesTask().execute();
	}
	
	private void reviewExam() {
		Intent i = new Intent(this, ExamResultDetailActivity.class);
		Bundle bundle = new Bundle();
		bundle.putParcelable("exam", mExam);
		i.putExtras(bundle);
		startActivity(i);
	}
	
	private void errorExam() {
		Exam errorExma = new Exam();
		for(Question q : mExam.getQuestions()) {
			if(!q.isBlank() && !q.isCorrect()) {
				Question newq = new Question();
				newq.setId(q.getId());
				newq.setSubjectId(q.getSubjectId());
				newq.setContent(q.getContent());
				newq.setType(q.getType());
				newq.setTypeName(q.getTypeName());
				newq.setRequired(q.getRequired());
				newq.setAnswer(q.getAnswer());
				errorExma.getQuestions().add(newq);
			}
		}
		Intent i = new Intent(ExamResultActivity.this, ErrorExamActivity.class);
		Bundle bundle = new Bundle();
		bundle.putParcelable("exam", errorExma);
		i.putExtras(bundle);
		startActivity(i);
	}
	
	class CalErrorTimesTask extends AsyncTask<Void, Void, Void> { //后台统计每道题目的答错次数

		@Override
		protected Void doInBackground(Void... arg0) {
			if(mExam != null) {
				mExam.statErrorTimes(ExamResultActivity.this);
			}
			return null;
		}
		
	}
	
	@Override
	public void finish() {
		startActivity(new Intent(ExamResultActivity.this, PreExamActivity.class));
	}
	
}
